import {
	v4 as uuidv4
} from 'uuid';
import {
	tenantId
} from '@/api/http/setting'

import {
	saveAttachAPI,
	removeAttachAPI
} from "@/api/api/my.js"
export default {
	data() {
		return {
			fileList: [],
			requestFileList: []
		}
	},
	methods: {
		deleteFun(event) {
			let index = event.index;
			this.fileList.splice(index, 1);
			let attachId = this.requestFileList[index].attachId;
			this.requestFileList.splice(index, 1);
			//removeAttachAPI(attachId);

		},
		async afterRead(event) {
			let fileArr = event.file;
			let fileList = this.fileList.concat(event.file);
			this.fileList = fileList;
			for (let i = 0; i < fileArr.length; i++) {
				let data = await this.$refs.upload.initClient();
				const host = 'https://iot-device-saas.oss-cn-shenzhen.aliyuncs.com';
				const signature = data.signature;
				const ossAccessKeyId = data.OSSAccessKeyId;
				const policy = data.policy;
				const securityToken = data.securityToken;

				let tempFile = fileArr[i];
				let filePath = tempFile.url;

				let date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let month2 = month < 10 ? "0" + month : month;
				let date2 = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
				let name = "";
				if (!tempFile.name) {
					let path = tempFile.url;
					let indexOf = path.lastIndexOf("/");
					name = path.substring(indexOf + 1);
				} else {
					name = tempFile.name;
				}
				let indexOf = name.lastIndexOf(".");
				let extension = name.substring(indexOf + 1);

				const key =
					`public/car/${tenantId}/${year}/${month2}/${date2}/${uuidv4()}.${extension}`;
				//const key = '/public/car/000000/2023/01/12/'+res.tempFiles[0].name;  //报错多了一根斜杠报错:InvalidObjectName
				uni.uploadFile({
					url: host, // 开发者服务器的URL。
					filePath: filePath, // 待上传文件的文件路径。
					name: 'file', // 必须填file。
					formData: {
						key,
						policy,
						'success_action_status': '200', //让服务端返回200,不然，默认会返回204
						OSSAccessKeyId: ossAccessKeyId,
						signature,
						'x-oss-security-token': securityToken // 使用STS签名时必传。
					},
					success: (res2) => {
						if (res2.statusCode === 200) {
							let link = host + "/" + key;
							saveAttachAPI({
								link: link,
								domainUrl: host,
								name: key,
								originalName: name,
								extension: extension,
								attachSize: tempFile.size
							}).then(([err, attach_data]) => {
								if (err) return;
								let currentRoutes = getCurrentPages(); // 获取当前打开过的页面路由数组
								let currentRoute = currentRoutes[currentRoutes.length - 1].route //获取当前页面路由
								let params = {};
								if(currentRoute == "pagesMy/views/maintainForm"){
									params = {
										attachId: attach_data,
										link: link,
										originalName: name,
										attachType: 1,
									};
								}else{
									params = {
										attachId: attach_data,
										link: link,
										originalName: name,
										attachCategory: 1,
										businessType: 1
									};
								}
								
								this.requestFileList.push(params);
							})
						}
					},
					fail: err => {
						console.log(err);
					}
				});
			}
		}
	}
}
